Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Runtime.InteropServices
Imports System.Configuration
Imports System.Text
Public Class FeedbackConts
    Public sURL As String
    Public nDescriptionID As Integer
    Public DBConnectstring As String
    Public sEmailaddress As String
    Public sMessageText As String
    Public sBrowserringURL As String
    Public sReferringURL As String
    Public sCompany As String
    Public ProductionName As String
    Private Rs As DataSet
    'Private Conn As SqlConnection
    Public ErrMsg As String


    Public Function feedbackform() As DataSet

        Dim sSQL As String
        Dim sErr As String = ""

        'Conn.Open(DBConnectstring)
        Rs = New DataSet
        
        sSQL = "select "
        sSQL = sSQL & " DescriptionID, "
        sSQL = sSQL & " DescriptionText "
        sSQL = sSQL & "from "
        sSQL = sSQL & " FEEDBACK_DESCRIPTIONS "
        sSQL = sSQL & "order by DisplayOrder"

        'Rs.Open(sSQL, Conn, adOpenForwardOnly, adLockReadOnly, adCmdText)
        Try
            GetDataset(DBConnectstring, sSQL, "feedback", sErr, Rs)
            If Rs.Tables(0).Rows.Count > 0 And Len(sErr) = 0 Then
                feedbackform = Rs

                Exit Function
            Else
                LogErrorEvent(sErr)

            End If

        Catch ex As SqlException
            'Dim strErrEx As String
            Dim strTDErr As String = ""
            Dim i As Integer

            For i = 0 To ex.Errors.Count - 1
                strTDErr = ex.Errors(i).Message.ToString()
            Next i
            ''/**use this code to debug**//
            'strError = ex.Message '& vbCrLf & " UserID :" & UserID & " Offending SQL error " & strTDErr & " : " & vbCrLf & strSQL & vbCrLf
            'strError = ExpError(strTDErr, ex)
            LogErrorEvent(strTDErr)
        End Try
        Return Rs
        
    End Function
    Public Function feedbackinfo(ByVal VID As String, ByVal CID As String) As Boolean


        Dim objCDOnts
        Dim sDescriptionText As String
        Dim sSQL As String
        Dim rst As DataSet
        Dim sMsg As String
        'Dim rst1 As DataSet
        Dim bFeed As Boolean
        Dim sErr As String = ""

        Dim Conn As SqlConnection = New SqlConnection(DBConnectstring)
        Dim i As Integer
        Conn.Open()
        Dim Cmd As SqlCommand = New SqlCommand("INSFEEDBACK", Conn)
        With Cmd

            .CommandType = CommandType.StoredProcedure

            '.Parameters.Add("@i_vid", VID)
            '.Parameters.Add("@i_cid", CInt(CID))

            '.Parameters.Add("@i_url", sURL)
            '.Parameters.Add("@i_descriptionid", nDescriptionID)
            '.Parameters.Add("@i_emailaddress", sEmailaddress)
            '.Parameters.Add("@i_messagetext", sMessageText)
            '.Parameters.Add("@i_browseros", sBrowserringURL)
            '.Parameters.Add("@i_referringurl", sReferringURL)
            '.Parameters.Add("@i_company", sCompany)
            '.Parameters.Add("@o_errorMsg", Nothing)
            '.Parameters(3).SqlDbType = SqlDbType.VarChar
            '.Parameters(3).Direction = ParameterDirection.Output
        End With

        Cmd.ExecuteNonQuery()
        '------------------------------------------------------------------------
        'Returns a message to the calling ASP page.
        '------------------------------------------------------------------------
        If Not IsNothing(Cmd.Parameters("o_errorMsg").Value) Then

            'Debug.Print("Error Msg = " & Cmd.Parameters("o_errorMsg"))
            ErrMsg = Cmd.Parameters("o_errorMsg").Value
            Conn.Close()
            LogErrorEvent(ErrMsg)



        End If
        '------------------------------------------------------------------------

        'If Not IsNull(Cmd.Parameters("o_errormsg")) Then
        '       Debug.Print ("Error Msg = " & Cmd.Parameters("0_errormsg"))
        '       Exit Function
        'Else
        '-----  getting DescriptionText for use in the notification email next.
        bFeed = True

        If bFeed Then
            sDescriptionText = "Uknown Description" 'default value, indicates error if not overwritten.
            Rs = New DataSet

            sSQL = "SELECT DescriptionText from FEEDBACK_DESCRIPTIONS where DescriptionID = " & nDescriptionID
            GetDataset(DBConnectstring, sSQL, "feedbck", sErr, Rs)

            If Rs.Tables(0).Rows.Count > 0 Then
                sDescriptionText = SafeString(Rs.Tables(0).Rows.Item("DescriptionText"))

            End If

            '-----  checking db for administrators who should receive email notification of the bug.
            rst = New DataSet

            sSQL = "SELECT EmailAddress, RealName FROM FEEDBACK_RECIPIENTS"
            GetDataset(DBConnectstring, sSQL, "feedbck1", sErr, rst)
            'rst.Open(sSQL, Conn, adOpenForwardOnly, adLockReadOnly, adCmdText)
            If rst.Tables(0).Rows.Count > 0 Then
                '-----  recipients exists.
                '-----  formatting message text.
                sMsg = ""
                sMsg = sMsg & ProductionName & " Merchant Feedback Submitted" & vbCrLf
                sMsg = sMsg & "Date/Time: " & FormatDateTime(Now, vbShortDate) & " - " & FormatDateTime(Now, vbLongTime) & vbCrLf
                sMsg = sMsg & "Merchant ID: " & VID & vbCrLf
                sMsg = sMsg & "Email Address: " & sEmailaddress & vbCrLf
                sMsg = sMsg & "URL: " & sURL & vbCrLf
                sMsg = sMsg & "Message: " & sURL & vbCrLf
                sMsg = sMsg & "---------------------------------------------" & vbCrLf
                sMsg = sMsg & sMessageText & vbCrLf
                sMsg = sMsg & "---------------------------------------------" & vbCrLf
                sMsg = sMsg & "Browser/OS: " & sBrowserringURL & vbCrLf
                '-----  sending email to everyone on the recipients list.
                'Do While Not rst.EOF
                For i = 0 To rst.Tables(0).Rows.Count - 1
                    objCDOnts = CreateObject("CDONTS.NewMail")
                    objCDOnts.From = "support@georgia-globaltp.com"
                    objCDOnts.To = Trim(rst.Tables(0).Rows(i).Item("EmailAddress"))
                    objCDOnts.Subject = ProductionName & " Merchant Feedback Submitted"
                    objCDOnts.Body = sMsg
                    objCDOnts.Send()
                    objCDOnts = Nothing
                Next
                'rst.MoveNext()
                'Loop
            End If

           
            feedbackinfo = bFeed

        End If
        Return bFeed
    End Function

End Class
